package com.cold.demo.rabbitmq.demo2.$2;

import com.cold.demo.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author: zyc
 */
public class Consumer2$2 {


    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtils.getConnection();
        Channel    channel    = connection.createChannel();
        // 绑定队列
        // 1、队列名称
        // 2、是否持久化消息
        // 3、是否独占队列
        // 4、是否自动删除队列
        // 5、参数–队列的其他属性（构造参数）
//        channel.queueDeclare("test1", false, false, false, new HashMap<>(0));

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            String uuid    = UUID.randomUUID().toString();
            System.out.println(String.format("接收到消息　（%s）：%s", uuid, message));
            try {
                doWork(message);
            } finally {
                System.out.println(String.format("处理消息完毕（%s）：%s", uuid, message));
            }
        };

        channel.basicConsume("test1", true, deliverCallback, consumerTag -> {
        });
    }

    private static void doWork(String task) {
        for (char ch : task.toCharArray()) {
            if (ch == '.') {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
